
last_str=''
depth=0

#返回第一个单词
function judge_frame()
{
	# ret=`echo $1 | awk '{if ($1 == "Breakpoint" ) {\
	# 						print $5;\
	# 					} \
	# 					else if(substr($1,1,1) == "#") {\
	# 						print "#"}\
	# 					}'`
	ret_1=`echo $1 | awk '{print $1}'`
	ret_2=`echo $1 | awk '{print $2}'`
	ret_3=`echo $1 | awk '{print $3}'`
	ret_4=`echo $1 | awk '{print $4}'`
	ret_5=`echo $1 | awk '{print $5}'`

	if [[ "$ret_3" =~ ^\( ]]; then
		ret_symbol=$ret_2
	elif [[ "$ret_4" =~ ^\( ]]; then
		ret_symbol=$ret_3
	elif [[ "$ret_5" =~ ^\( ]]; then
		ret_symbol=$ret_4
	fi
}

function parse_gdblog() {
	while read line
	do
		judge_frame "$line"
		if [[ "$ret_1" =~ \# ]]; then
			#并且编号为#0
			if [[ $ret_1 == "#0" ]]; then
				for (( i=0; i<$depth; i=i+1 )); do
					echo -n '    '
				done
				if [[ $last_str != " " && -n $last_str ]]; then
					echo $last_str
				fi
				depth=0
				#更新last_str
				last_str=$ret_symbol
			fi
			depth=$depth+1
		fi
	done
}

function main() {

	if [ $1 == "parse_gdblog" ]; then
		parse_gdblog
		return;
	fi
	filename=$1
	#获取nm的Tt类型符号
	nm $1| awk '{if(($2=="T" || $2=="t" || $2=="i" || $2=="I" ) && ($3 != "$x")) print $0}' > symbol
	gdb ${filename} -x gdb.cmd | bash parse.sh parse_gdblog | tee run_trace.txt

}

#
# 调用主函数
#
main $*
